<template>
	<view>
		<!-- 收货地址 -->
		<view class="addr" @tap="selectAddress">
			<view class="icon">
				<image src="../../static/img/addricon.png" mode=""></image>
			</view>
			<view class="right" v-if="recinfo != ''">
				<view class="tel-name">
					<view class="name">
						{{recinfo.name}}
					</view>
					<view class="tel">
						{{recinfo.phone}}
					</view>
				</view>
				<view class="addres">
					{{recinfo.area_name}}
					{{recinfo.address}}
				</view>
			</view>
			<view class="right" v-else>
				请选择地址
			</view>
		</view>
		<!-- 购买商品列表 -->
		<view class="buy-list">
			<view class="row" v-for="(row,index) in buylist" :key="index">
				<view class="goods-info">
					<view class="img">
						<image :src="row.img_src |getImg"></image>
					</view>
					<view class="info">
						<view class="title">{{row.goodsinfo.name}}</view>
						<view class="spec">选择 : {{row.attribute_name}} ; 数量 : {{row.num}}</view>
						<view class="price-number">
							<view class="price">￥{{row.price*row.num}}</view>
							<view class="number">

							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 提示-备注 -->
		<!-- <view class="order">
			<view class="row">
				<view class="left">
					积分 :
				</view>
				<view class="right">
					已扣除{{int}}积分抵扣{{deduction|toFixed}}元
				</view>
			</view>
			<view class="row">
				<view class="left">
					备注 :
				</view>
				<view class="right">
					<input placeholder="选填,备注内容" v-model="note" />
				</view>
			</view>
		</view> -->
		<!-- 明细 -->
		<view class="detail">
			<view class="row">
				<view class="nominal">
					商品金额
				</view>
				<view class="content">
					￥{{goodsPrice|toFixed}}
				</view>
			</view>
			<view class="row">
				<view class="nominal">
					邮费
				</view>
				<view class="content">
					￥+{{freight|toFixed}}
				</view>
			</view>
			<!-- <view class="row">
				<view class="nominal">
					积分抵扣
				</view>
				<view class="content">
					￥-{{deduction|toFixed}}
				</view>
			</view> -->
		</view>
		<view class="blck">

		</view>
		<view class="footer">
			<view class="settlement">
				<view class="sum">合计:<view class="money">￥{{sumPrice|toFixed}}</view>
				</view>
				<view class="btn" @tap="toPay">提交订单</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		CreateOrder,
		OrderFound
	} from "@/api/api.js"
	export default {
		data() {
			return {
				buylist: [], //订单列表
				goodsPrice: 0.0, //商品合计价格
				// sumPrice: 0.0, //用户付款价格
				// freight: 0.00, //运费(计算属性)
				note: '', //备注
				int: 1200, //抵扣积分
				deduction: 0, //抵扣价格
				recinfo: [],
				area: ''

			};
		},
		onLoad() {
			this.OrderFound()
		},
		onShow() {
			//页面显示时，加载订单信息
			setTimeout(() => {
				uni.getStorage({
					key: 'goodsdata',
					success: (ret) => {
						this.buylist = ret.data;
						this.goodsPrice = 0;
						this.buylist.forEach(item => {
							this.goodsPrice = this.goodsPrice + (item.num * item.price);
						})
					}
				});
			}, 400)

		},
		computed: {
			freight() {
				let area = ''
				var postage = 0
				if (this.recinfo != '' && this.buylist != '') {
					let arr = this.recinfo.area_name.split('-')
					area = arr[0] //收货地址省份
					this.buylist.forEach(item => {
						if (item.goodsinfo.post_area != '') {
							let arr = item.goodsinfo.post_area.split(',')
							let findarea = arr.findIndex(item => item == area)
							if (findarea != -1) {
								postage = Number(postage) + Number(item.goodsinfo.postage)
							}
						}
					})
				}
				return postage
			},
			sumPrice() {
				return Number(this.goodsPrice) + Number(this.freight);
			}
		},
		onBackPress() {
			//页面后退时候，清除订单信息
			this.clearOrder();
		},
		filters: {
			toFixed: function(x) {
				return parseFloat(x).toFixed(2);
			}
		},
		methods: {
			OrderFound() {
				OrderFound().then(res => {
					if (res.data.address != '') {
						this.recinfo = res.data.address
					}
				})
			},
			clearOrder() {
				uni.removeStorage({
					key: 'buylist',
					success: (res) => {
						this.buylist = [];
					}
				});
			},
			toPay() {
				//本地模拟订单提交UI效果
				uni.showLoading({
					title: '正在提交订单...'
				})
				CreateOrder({
					address_id: this.recinfo.id,
					data: this.buylist
				}).then(res => {
					setTimeout(() => {
						uni.hideLoading();
						uni.showToast({
							title: res.message,
							icon: "none"
						})
						if (res.status == 1) {
							uni.redirectTo({
								url: "../pay/payment/payment?amount=" + this.sumPrice +
									"&order_id=" + res.data.order_id
							})
						}
					}, 1000)
				})
			},
			//选择收货地址
			selectAddress() {
				uni.navigateTo({
					url: '../user/address/address?type=select'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #0F172A;
		padding-top: 30upx;
	}

	.addr {
		width: 86%;
		padding: 20upx 3%;
		margin: 0 auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		display: flex;
		background: #1E293B;
		color: #fff;

		.icon {
			width: 80upx;
			height: 80upx;
			display: flex;
			align-items: center;

			image {
				width: 60upx;
				height: 60upx;
			}
		}

		.tel-name {
			width: 100%;
			display: flex;
			font-size: 32upx;

			.tel {
				margin-left: 40upx;
			}
		}

		.addres {
			width: 100%;
			font-size: 26upx;
			color: #fff;
		}
	}

	.buy-list {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		background: #1E293B;
		color: #fff;

		.row {
			margin: 30upx 0;

			.goods-info {
				width: 100%;
				display: flex;

				.img {
					width: 90px;
					height: 90px;
					border-radius: 10upx;
					overflow: hidden;
					flex-shrink: 0;
					margin-right: 10upx;

					image {
						width: 90px;
						height: 90px;
					}
				}

				.info {
					width: calc(100% - 90px);
					padding-left: 10px;
					box-sizing: border-box;
					height: 90px;
					overflow: hidden;
					display: flex;
					flex-wrap: wrap;
					position: relative;

					.title {
						width: 100%;
						font-size: 28upx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						// text-align: justify;
						overflow: hidden;
					}

					.spec {
						font-size: 12px;
						background-color: #8980FD;
						color: #fff;
						height: 40upx;
						display: flex;
						align-items: center;
						padding: 1px 10px;
						border-radius: 20upx;
						margin-bottom: 50px;
						margin-top: 10px;
					}

					.price-number {
						position: absolute;
						width: 100%;
						bottom: 0upx;
						display: flex;
						justify-content: space-between;
						align-items: flex-end;
						font-size: 28upx;
						height: 40upx;

						.price {
							color: #fff;
							font-size: 30upx;
							font-weight: bold;
						}

						.number {
							display: flex;
							justify-content: center;
							align-items: center;

						}
					}
				}
			}
		}
	}

	.order {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		background: #1E293B;
		color: #fff;

		.row {
			margin: 20upx 0;
			height: 40upx;
			display: flex;
			padding: 10px;

			.left {
				font-size: 28upx;
			}

			.right {
				margin-left: 40upx;
				font-size: 26upx;

				input {
					font-size: 26upx;
					color: #fff;
				}
			}
		}
	}

	.blck {
		width: 100%;
		height: 100upx;
	}

	.footer {
		width: 92%;
		padding: 0 4%;
		background-color: #1E293B;
		height: 110upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		padding-top: 5px;
		font-size: 28upx;
		position: fixed;
		bottom: 0upx;
		z-index: 5;
		border-top-right-radius: 20px;
		border-top-left-radius: 20px;

		.settlement {
			width: 80%;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			color: #fff;

			.sum {
				width: 50%;
				font-size: 28upx;
				margin-right: 10upx;
				display: flex;
				justify-content: flex-end;

				.money {
					font-weight: 600;
				}
			}

			.btn {
				padding: 0 40upx;
				height: 70upx;
				background-color: #798AD6;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30upx;
				border-radius: 40upx;
			}
		}
	}

	.detail {
		width: 86%;
		padding: 10upx 3%;
		margin: 30upx auto 20upx auto;
		box-shadow: 0upx 5upx 20upx rgba(0, 0, 0, 0.1);
		border-radius: 20upx;
		background: #1E293B;
		color: #fff;

		.row {
			height: 60upx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 10px;

			.nominal {
				font-size: 28upx;
			}

			.content {
				font-size: 14px;
				color: #8e9eb5;
			}
		}
	}
</style>